﻿using System;
using System.Collections.Generic;
using YGWebMobileApi.Data;
using OneMile.Common;
using OneMile.Entity.Model;
using OneMile.BLL;

namespace YGWebMobileApi.APIs
{
     [MobileAPI(Code = "MC0301", Name = "用户获取订单列表(分页)")]
    public class UGetOrderListByUserId_S: BaseAPI, IProcessMessage
    {
        private BLLUOrder bll;
        public BLLUOrder BLL
        {
            get
            {
                if (bll == null)
                    bll = BLLFactory.getInstance().CreateUOrderBLL();
                return bll;
            }
        }
        #region IProcessMessage 成员

        public MobileRspMsg ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);

                string UserId = reqMsg.Body.GetFieldValue("UserId");
                int PageNo = reqMsg.Head.PageNo;
                int PageSize = reqMsg.Head.PageSize;
                string shopsType = reqMsg.Body.GetFieldValue("ShopsType");
                string OrderType = reqMsg.Body.GetFieldValue("OrderType");
                if (String.IsNullOrEmpty(UserId) || PageNo == 0 || PageSize == 0 || String.IsNullOrEmpty(OrderType))
                {
                    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                    return rspMsg;
                }

                List<EOrderForList> list = (List<EOrderForList>)BLL.GetOrderListByUserId(UserId, PageSize, PageNo, shopsType, OrderType.TInt32());

                for (int i = 0; i < list.Count; i++)
                {
                    var fll = rspMsg.Body.AddFieldList("Order");
                    fll.AddField("OrderId", list[i].OrderId.TString());
                    fll.AddField("ShopsName", list[i].ShopsName);
                    fll.AddField("ShopsType", list[i].ShopsType);
                    fll.AddField("TotalPrice", list[i].TotalPrice.TString());
                    fll.AddField("OrderType", list[i].OrderType.TString());
                    fll.AddField("Rec_CreateTime", list[i].Rec_CreateTime.TDateTimeString());
                    fll.AddField("Rec_ModifyTime", list[i].Rec_ModifyTime.TDateTimeString());
                    fll.AddField("OrderAddress", list[i].OrderAddress);
                    fll.AddField("Commodities", list[i].Commodities);
                }

                rspMsg.Head.PageNo = PageNo;
                rspMsg.Head.PageSize = PageSize;

                rspMsg.Head.RspCode = MessageCode.Success.Code;
                rspMsg.Head.RspMsg = MessageCode.Success.Msg;

                return rspMsg;
            }
            catch (Exception e)
            {
                ErrorLogWriter.Write("UGetOrderListByUserId_S-" + e);
                throw e;
            }
        }

        #endregion
    }
}